﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Configuration;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Net.Mime;

namespace SendMail
{
    public class MailUtility
    {
        public string From { get; set; }
        public List<string> To { get; set; }
        public List<string> CC { get; set; }
        public List<string> BCC { get; set; }

        public string Subject { get; set; }
        public string Body { get; set; }
        public List<string> Attachments { get; set; }

        public string SmtpHost { get; set; }
        public string SmtpUsername { get; set; }
        public string SmtpPassword { get; set; }
        public int Port { get; set; }
        public bool EnableSSL { get; set; }

        public MailUtility()
        {
            To = new List<string>();
            CC=new List<string>();
            BCC=new List<string>();
            Attachments=new List<string>();
        }

        public void SendMail()
        {
            MailMessage mail = new MailMessage();
            //From = "arri.luca@gmail.com";
            mail.From = new MailAddress(From);

            foreach (string dest in To)
                mail.To.Add(new MailAddress(dest));

            foreach (string dest in CC)
                mail.CC.Add(new MailAddress(dest));

            foreach (string dest in BCC)
                mail.Bcc.Add(new MailAddress(dest));

            mail.Subject = Subject;
            mail.Body = Body;

            foreach (string attachment in Attachments)
                mail.Attachments.Add(new Attachment(attachment));


            //SmtpHost = "smtp.gmail.com";
            SmtpClient smtp = new SmtpClient(SmtpHost);

            //SmtpUsername = "arri.luca@gmail.com";
            //SmtpPassword = "faeltuor";

            smtp.UseDefaultCredentials = false;
            smtp.EnableSsl = EnableSSL;
            smtp.Port = Port;
            //smtp.EnableSsl = true;
            //smtp.Port = 25;

            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

            //smtp.Timeout = 30000;
            NetworkCredential net = new NetworkCredential(SmtpUsername, SmtpPassword);
            smtp.Credentials = net;
            try
            {
                smtp.Send(mail);
                return;
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }

        }
    }
}
